<?php
namespace app\blog\model;
use think\db;
use think\Model;
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
class GoodsModel extends Model
{

    public function __construct($data = [])
    {
        parent::__construct($data);
        $this->session=$this->session();
    }

    /*
     * 商品列表
     * */
    public function session()
    {
        $id=cmf_get_current_admin_id();
        $where['agency_id']=$id;
        $session=DB::name('shop')->where($where)->find();
        return $session;
    }
    public function index($cause)
    {
        $session=$this->session();
        $cause['go.shop_id']=$session['id'];
        $data=DB::name('goods')->alias('go')->join(' goodsclass gc ','go.g_category = gc.id')->join('format f','f.f_id = go.g_format')->join('brand b','go.brand_id = b.id')->where($cause)->field('go.*,gc.gc_name,f.f_name,b.name')->paginate(10);
        return $data;
    }
    /*规格查询*/
    public function category($where=null,$state=null)
    {
        $session=$this->session();
        $where['shop_id']=$session['id'];
        $where['status']=1;
        $category=DB::name('goodsclass')->where($where)->select()->toArray();
        $data['id']=0;
        $data['gc_name']='全部';
        if($state == 1)
        {
            if(!isset($where['id']))
            {
                foreach ($category as $k=>$v)
                {
                    $category[0]=$data;
                    $category[$k+1]=$v;
                }
            }
        }
        return $category;

    }
    /*系列查询*/
    public function brand()
    {
        $session=$this->session();
        $where['shop_id']=$session['id'];
        $where['status']=1;
        $brand=DB::name('brand')->where($where)->select()->toArray();
        return $brand;
    }
    /*
     * 规格查询*/
    public function format()
    {
        $session=$this->session();
        $where['shop_id']=$session['id'];
        $where['status']=1;
        $brand=DB::name('format')->where($where)->select()->toArray();
        return $brand;
    }
    /*
    * 添加商品
    * */
    public function add($array)
    {
        $session=$this->session();
        $date['goods_guid']=guid();
        $date['goods_name']=$array['goodsname'];
        if(isset($array['pic'])){
            if($array['pic'][0])
            {
                $date['goods_prc']=$array['pic'][0];
            }else{
                $date['goods_prc']='';
            }
        }else{
            $date['goods_prc']='';
        }
        $date['g_format']=$array['format'];
        $date['g_category']=$array['category'];
        $date['shop_id']=$session['id'];
        $date['brand_id']=$array['brandid'];
        $date['g_price']=$array['price'];
        $date['g_newprice']=$array['vipprice'];
        $date['status']=$array['statue'];
        $date['number']=$array['stock'];
        $date['c_time']=time();
        $date['desc']=$array['desc'];
        $date['appointment']=$array['appointment'];
        $state=DB::name('goods')->insert($date);
        return $state;
    }
    /*
    * 更改商品
    * */
    public function detail($date)
    {
        $session=$this->session();
        $where['shop_id']=$session['id'];
        $where['g_id']=$date['g_id'];
        $date['goods_prc']=substr($date['goods_prc'],strrpos($date['goods_prc'],'blog'));
        $date['u_time']=time();
        $state=DB::name('goods')->where($where)->update($date);
        return $state;

    }

    /*
    * 删除类别
    * 查询是否有该订单，有订单的不让删除
    * */
    public function del($date)
    {
        $session=$this->session();
       $where['goods_id']=$date['id'];
       $where['shop_id']=$session['id'];
       $order_detail=DB::name('order_detail')->where($where)->find();
       if($order_detail)
       {
           $state['num']=0;
           $state['con']='订单里存在该商品，建议下架!';
       }else{
           $cause['g_id']=$date['id'];
           $goods=DB::name('goods')->delete($cause);
           if($goods)
           {
               $state['num']=$goods;
               $state['con']='删除成功';
           }else{
               $state['num']=$goods;
               $state['con']='删除失败';
           }
       }
       return $state;

    }


}